package cn.edu.zafu.bigdata.xct.wlbc;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UDPTimeClient {
    public static void main(String[] args) {
        String host = "192.168.16.54"; // 替换为你同学的服务端IP地址
        int serverPort = 12345; // 设置服务端的端口号

        try (DatagramSocket clientSocket = new DatagramSocket()) {
            String sentence = "What time is it?";
            byte[] sendData = sentence.getBytes();
            byte[] receiveData = new byte[1024];

            InetAddress IPAddress = InetAddress.getByName(host);

            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, serverPort);
            clientSocket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("服务端时间: " + response);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}